package org.wattdepot.cli.commands;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

/**
 * This class tests the carbon content command of the 
 * CommandLineInterface's actual commands.
 * @author David Mau
 */
public class TestCommandCarbonContent {
  String cr = System.getProperty("line.separator");
  
  /**
   * Test normal operation of the carbon content command.
   */
  @Test
  public void testCarbonContents() {
    // Create a new carbon content command object.
    CarbonContentCommand command = new CarbonContentCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_WAIAU_8", "2009-11-15T12:00:00.000-10:00", "30" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Ready the output that we know is true
    String knownOutput = "1800.0 pounds CO2/MegaWatt-Hours";
    knownOutput = knownOutput.concat(cr);
    
    // Compare the two to see if they are the same.
    int result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known result at Waiau #8 with good time stamp", result, 0);
    
    // Create a new List to store the args
    args = Arrays.asList( "SIM_IPP", "2009-11-15T12:00:00.000-10:00", "30" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );

    // Ready the output that we know is true
    knownOutput = "2083.157894736842 pounds CO2/MegaWatt-Hours";
    knownOutput = knownOutput.concat(cr);
    
    // Compare the two to see if they are the same.
    result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known result at IPP with good time stamp", result, 0);
  }

  /**
   * Test bad time stamp detection for the carbon content command.
   */
  @Test
  public void testCarbonContentsBadTime() {
    // Create a new carbon content command object.
    CarbonContentCommand command = new CarbonContentCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_AES", "PREHISTORY", "10" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad timestamp", sourceOutput
        .contains(WattDepotCliCommand.badTimeStamp));

  }

  /**
   * Test bad source detection for the carbon content command.
   */
  @Test
  public void testCarbonContentBadSource() {
    // Create a new carbon content command object.
    CarbonContentCommand command = new CarbonContentCommand(null);
    
    // Attempt to use a bad source with carbon
    // Create a new List to store the args
    List<String> args = Arrays.asList( "PLUTO", "2009-11-01T12:00:00.000-10:00", "30" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad source", sourceOutput
        .contains(WattDepotCliCommand.resourceNotFound));
  }

  /**
   * Test a bad interval given for the carbon content command.
   * The given String should be convertible into an Integer.
   */
  @Test
  public void testCarbonContentBadInterval() {
    // Attempt to use a bad interval
    // Create a new carbon content command object.
    CarbonContentCommand command = new CarbonContentCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_KAHE", "2009-11-02T12:00:00.000-10:00", "SOME" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad interval", sourceOutput
        .contains(WattDepotCliCommand.intervalNotNumber));
    
  }

  /**
   * Test another bad interval given for the carbon content command.
   * The given String should be a non-zero, positive integer.
   */
  @Test
  public void testCarbonContentBadIntervalInteger() {
    // Create a new carbon content command object.
    CarbonContentCommand command = new CarbonContentCommand(null);
    
    // Attempt to use a negative interval
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_OAHU_GRID", "2009-11-03T12:00:00.000-10:00", "-1" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a negative interval", sourceOutput
        .contains(WattDepotCliCommand.intervalNegativeOrZero));
    
    // Attempt to use zero interval
    // Create a new List to store the args
    args = Arrays.asList( "SIM_AES", "2009-11-04T12:00:00.000-10:00", "0" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a zero interval", sourceOutput
        .contains(WattDepotCliCommand.intervalNegativeOrZero));
  }  

  /**
   * Tests to see if the carbon content command can handle excess arguments.
   */
  @Test
  public void testExcessArguments() {
    // Create a new carbon content command object.
    CarbonContentCommand command = new CarbonContentCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1", "2", "3", "4");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking excess carbon content argument handling", 
        sourceOutput.contains(WattDepotCliCommand.tooManyArguments));
  }
  
  /**
   * Tests to see if the carbon content command can handle not enough arguments.
   */
  @Test
  public void testFewArguments() {
    // Create a new carbon content command object.
    CarbonContentCommand command = new CarbonContentCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking carbon content handling not enough arguments", 
        sourceOutput.contains(WattDepotCliCommand.missingArguments));
  }
}
